package 数组和链表;


import 抽象数据类型.ListNode;

class 链表中倒数第k个结点 {

    public static ListNode FindKthToTail(ListNode head, int k) {
        ListNode fast = head;
        while (k-- != 0) {
            if (fast == null) {
                return null;
            }
            fast = fast.next;
        }
        ListNode cur = head;
        while (fast != null) {
            fast = fast.next;
            cur = cur.next;
        }
        return cur;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        node.next.next = new ListNode(3);
        System.out.println((FindKthToTail(node, 1)).val);
        /**
         测试输入链表1，2，3，倒数第二个数
         输出2
         */

    }

}
